package com.zhongbei.controller;


import com.zhongbei.service.NewsService;
import com.zhongbei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.zhongbei.pojo.New;

@RestController
@RequestMapping("/enshuu")
public class NewsController {
    @Autowired
    NewsService newsService;

    @PostMapping("/addnew")
    public Result<String> addNews(@RequestBody New nw){
        int i = newsService.addNews(nw);
        if (i==1){
            return Result.success("发布成功");
        }else{
            return Result.error("发布失败");
        }
    }
    @GetMapping("/getnews")
    public Result<List<New>> getNews(){
        List<New> news = newsService.getNews();
        if (news==null){
            return Result.error("获取失败");
        }else{
            return Result.success(news);
        }
    }
    @PostMapping("/searchnews")
    public Result<List<New>> searchNews(@RequestBody String keyword){
        System.out.println("搜索关键词: " + keyword);
        List<New> i=newsService.searchNews(keyword);
        if (i==null){
            return Result.error("查询失败");
        }else{
            return Result.success(i);
        }
    }
    @PostMapping("/delnews")
    public Result<String> delNews(@RequestBody int id){
        int i=newsService.deleteNews(id);
        if (i==1){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
    @PostMapping("updatenews")
    public Result<String> updateNews(@RequestBody New nw){
        int i= newsService.changeNews(nw);
        if (i==1){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

}
